// index.js
// const app = getApp()
import request from "../../utils/request"

Page({
  onUpdateData(e) {
    // 在这里更新父组件的数据
    console.log(e)
    this.setData({ language: e.detail.newData });
    wx.setStorage({
      key: 'language',
      data: this.data.language,
      success: function (res) {
      }
    });
  },
  onLanguageTap() {
    const that = this;
    wx.showActionSheet({
        itemList: ['English', '中文','繁体'],
        success(res) {
            if (res.tapIndex === 0) {
                that.setData({ language: 'en' });
            } else if (res.tapIndex === 1) {
                that.setData({ language: 'zh' });
            }else{
              that.setData({language:'tr'})
            }
            wx.setStorage({
              key: 'language',
              data: that.data.language,
              success: function (res) {
              }
            });
        }
    });
  },
  
  data: {
    venueisshow:false,
    chooosedata:'',
    gridList:[],
    language:'zh',
    itemData1:'',
    backgroundcolor:'',
    is_seat_phone:'',
    is_blur_query:"",
    imgArr: [
      '../../images/会场布置图.png',
    ],
    unit_id:'',
    inputValue:[
      '',''
    ],
    originalData:[],
    filteredData:[],
    isShow:false,
    flowList: [],
    mettingAddrss: '',
    mettingTime: '',
    bagImg: '',
    logImg: '',
    venue_src: '',
    company_name: '',
    event_desc:'',
    event_name:'',
    eventDescCustom:{},
    eventNameCustom:{},
    search_bottom_text:'',
    searchCustom:{},//按钮,
    timeAddressCustom:{},//时间地点
    query_cond:[],//查询提示
    processCustom:{},
    companyCustom:{},
    event_time_address_show:'',
    process_show:'',
    query: {
      name: '',
      query:'',

    },
    index:'',
    unit_id:'',
    checkedname:'',
    queryconditions:'',
  },
  handleInput1:function(e){
    console.log(e)
    const itemData = e.currentTarget.dataset.item.value;
    if(itemData === '姓名'){
      

       this.setData({
         
         isShow:true
       })
    }else{
      this.setData({
        isShow:false
      })
    }
   

  },
  handleInput:function(e){
    
    const value = e.detail.value;
    console.log(value)
    console.log(e.currentTarget.dataset.index)
    if(value.length>0){
      const filteredData = this.data.originalData.filter(item=>{
        return item.value.toString().includes(value);
      })
        this.setData({
          filteredData:filteredData
        })
        
    }else{
    this.setData({
      filteredData:[]
      })  
    }
    
  },

  handleViewTap: function(e) {
    // 获取点击的 view 的内容
    const index = 0

    this.setData({
      chooosedata:e.target.dataset.name,

      [`inputValue[${index}]`]:e.target.dataset.name,


      isShow:false,
    })
    console.log(this.data.inputValue,"10086")
    console.log(this.data.isShow)
    // 这里可以添加处理点击事件的代码
  },
  
  previewImage(e) {
    // 图片预览
    console.log(e)
    // let indx = e.currentTarget.dataset.index // 找到点击图片的索引位置
    wx.previewImage({
      // current: indx,// 当前显示图片的http链接，将点击图片的索引数指定好
      urls: this.data.imgArr, // 需要预览的图片http链接列表
    })
  },
  async getPhoneNumber(e) {
    console.log(e);
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
      // 用户点击允许，获取到 encryptedData 和 iv
      const { encryptedData, iv } = e.detail;
  
      // 调用 wx.login 获取 code
      wx.login({
        success: (loginRes) => {
          if (loginRes.code) {
            // 将 code 发送到后端服务器
            console.log(loginRes.code,encryptedData,iv)
            wx.request({
              url: 'https://www.femx.cn/api/query/getuserphone', // 完整的后端接口 URL
              method: 'POST',
              data: {
                code: loginRes.code,
                encryptedData: encryptedData,
                iv: iv,
                unit_id:this.data.unit_id
              },
              success: async(res) => {
                // 接收后端服务器返回的手机号
                console.log(res)
                console.log('用户手机号:', res.data.phone)
                var phone = res.data.phone
                const resphone = await request({
                  url: "/checkphone",
                  data: {
                    id: this.data.index,
                    phone:phone,
                    type:1,
                  },
                  header: { 'content-type': 'application/x-www-form-urlencoded'},
                })
                console.log(resphone)
                if (resphone.code === 200) {
                  wx.navigateTo({
                    url: '/pages/query/query?param1='+JSON.stringify(resphone.data)
                  })
                }else{
                  wx.showModal({
                    title: '查询失败',
                    content: `${resphone.msg}`,
                    complete: (res) => {
                      if (res.cancel) {
                        console.log("点击取消")
                      }
                  
                      if (res.confirm) {
                        console.log("点击确认")
                      }
                    }
                  })
                }
                
              },
              fail: (error) => {
                console.error('获取手机号失败:', error);
              }
            });
          } else {
            console.log('登录失败！' + loginRes.errMsg);
          }
        }
      });
    }
  },

  onLanguageTap() {
    const that = this;
    wx.showActionSheet({
        itemList: ['English', '中文','繁体'],
        success(res) {
            if (res.tapIndex === 0) {
                that.setData({ language: 'en' });
            } else if (res.tapIndex === 1) {
                that.setData({ language: 'zh' });
            }else{
              that.setData({language:'tr'})
            }
            wx.setStorage({
              key: 'language',
              data: that.data.language,
              success: function (res) {
              }
            });
        }
    });
  },
      
  
  async onSubmit(e) {
    // 提交信息进行判断
    // console.log(e)
    var name = e.detail.value.name;
    console.log(e.detail.value);
    var phone = e.detail.value.query;

    console.log(this.data.query_cond[0].checked)
    if (!name&&this.data.query_cond[0].checked) {
      wx.showModal({
        title: '提示', // 对话框标题
        content: '请输入姓名', // 对话框内容
        showCancel: false, // 是否显示取消按钮
        confirmText: '确定', // 确定按钮的文字，默认为确定
        confirmColor: '#3CC51F', // 确定按钮的文字颜色，默认为绿色
      })
      return false;
    }else if(!phone&&this.data.query_cond[1].checked){
      wx.showModal({
        title: '提示', // 对话框标题
        content: '请输入查询条件', // 对话框内容
        showCancel: false, // 是否显示取消按钮
        confirmText: '确定', // 确定按钮的文字，默认为确定
        confirmColor: '#3CC51F', // 确定按钮的文字颜色，默认为绿色
      })
      return false;

    }
  
    this.setData({
      'query.name': name,
      'query.query':phone,
    })
    // console.log(this.data.query.name, "22222222222")
    const res1 = await request({
      url: "/meetingcheck",
      data: {
        id: this.data.index,
        query: JSON.stringify(this.data.query)
      }
    })
    console.log(res1, "5555556666111")
    if (res1.code === 200) {
      wx.navigateTo({
        url: '/pages/query/query?param1='+JSON.stringify(res1.data)
      })
    } else {
      wx.showModal({
        title: '提示', // 对话框标题
        content: '数据不存在，如有问题请自行联系发布者或管理员', // 对话框内容
        showCancel: false, // 是否显示取消按钮
        confirmText: '确定', // 确定按钮的文字，默认为确定
        confirmColor: '#3CC51F', // 确定按钮的文字颜色，默认为绿色
      })
      return false;
    }
},
async onLoad(options) {
  // 先解析跳转参数
  // 接口拿数据
  var that = this
  wx.getStorage({
    key: 'unitId', // 你要获取的数据的 key
    success (res) {
      console.log(res.data,"jjjj") // 成功回调，res.data 是你获取的数据
      that.setData({
        unit_id:res.data
      })
    },
    fail (err) {
      console.log(err) // 失败回调
    }
  })
  wx.getStorage({
    key: 'is_seat_phone', // 你要获取的数据的 key
    success:(res)=> {
     this.setData({
      is_seat_phone:res.data
     })
     console.log(this.data.is_applet_phone) // 成功回调，res.data 是你获取的数据
    },
    fail (err) {
      console.log(err) // 失败回调
    }
  })
  const index = JSON.parse(options.param1)
  console.log(index, "6666index")
  this.setData({
    index:index
  })
  const res2 = await request({
    url: "/meetingdetail",
    data: {
      id: index,
    }
  })
  if(res2.code !== 200){
    wx.showModal({
      title: '提示', // 对话框标题
      content: res2.msg, // 对话框内容
      confirmText: '确定', // 确定按钮的文字，默认为确定
      showCancel: false,//是否显示取消按钮
      confirmColor: '#3CC51F', // 确定按钮的文字颜色，默认为绿色
      async success (res) {
          
        if (res.confirm) {
          wx.navigateBack({
            url: '/pages/index/index'
          })
        } 
      }
    })
  }
  if(res2.code=== 200 && res2.data.restaurants){
    this.setData({
      originalData:res2.data.restaurants,
      filteredData:[]
    })
  }
  const prefix = "https://www.femx.cn"
  if(res2.data.content.background_current_type==='color')
  {
    this.setData({
      backgroundcolor:res2.data.content.background_current
    })
  }else{
    this.setData({
      bagImg: prefix + res2.data.content.background_current_img,
    })
  }
  if(res2.data.content.event_name_show){
    this.setData({
      event_name:res2.data.content.event_name,
    }) 
   }
    if(res2.data.content.logo_show){
      this.setData({
        logImg: prefix + res2.data.content.logo_src,

      })
    }
    if(res2.data.content.event_name_show){
      this.setData({
        mettingAddrss: res2.data.content.event_address,
        mettingTime: res2.data.content.event_time,
      })
    }
    if(res2.data.content.venue_show){
      this.setData({
        venue_src: prefix + res2.data.content.venue_src,
        venueisshow:true
      })

    }
    if(res2.data.content.process_show){
      this.setData({
        flowList: res2.data.content.process_list,

      })
    }



  this.setData({
    'imgArr[0]': prefix + res2.data.content.venue_src,
    company_name: res2.data.content.company_name,
    eventDescCustom:res2.data.content.eventDescCustom,
    eventNameCustom:res2.data.content.eventNameCustom,
    searchCustom:res2.data.content.searchCustom,
    timeAddressCustom:res2.data.content.timeAddressCustom,
    query_cond:res2.data.query_cond,

    search_bottom_text:res2.data.content.search_bottom_text,
    event_desc:res2.data.content.event_desc,
    processCustom:res2.data.content.processCustom,
    companyCustom:res2.data.content.companyCustom,
    process_show:res2.data.content.process_show,
    event_time_address_show:res2.data.content.event_time_address_show,
    is_blur_query:res2.data.content.is_blur_query

  })
  console.log(res2.data.query_cond  )
  console.log(this.data.query_cond)
  // https://www.femx.cn/
  
  console.log(res2, "6666")
  const app = getApp();
  this.setData({ gridList: app.globalData.lang });
  wx.getStorage({
    key: 'language',
    success:(res)=> {
     this.setData({
       language:res.data
     })
    }
  });
  },

});